﻿//------------------------------formFormulaEditor--------------------------------------start
(function ($) {
    //1.定义jquery的扩展方法combobox
    $.fn.formFormulaEditor = function (options, param) {
        if (typeof options == 'string') {
            return $.fn.formFormulaEditor.methods[options](this, param);
        }
        
        options = options || {};
        return this.each(function () {
            var state = $.data(this, 'formFormulaEditor');
            if (state) {
                $.extend(state.options, options);
            } else {
                $.data(this, 'formFormulaEditor', {
                    options: $.extend({}, $.fn.formFormulaEditor.defaults, options)
                });
            }

            var target = $(this);
            target.attr('contenteditable', 'true');
            target.bind("keyup", function (e) {
                if (/*e.ctrlKey && */e.keyCode == '219') {
                    var pos = getSelectionCoords();
                    var opts = $.data(this, 'formFormulaEditor').options;
                    layer.open({
                        type: 2,
                        title: '',
                        //skin: 'layui-layer-lan',
                        area: [options.width, options.height], //宽高
                        offset: [pos.y + 10, pos.x + 10],
                        content: ['/AutoUI/Sys/FormParam/List?UICode=' + opts.UICode],
                        anim: 5,
                        shade: [0],
                        closeBtn: 0,
                        shadeClose: true,
                        end: function () {
                            if (layer.windowResult)
                                insertHtmlAtCaret(layer.windowResult + '}');
                            target.focus();
                        }
                        //maxmin: params.maxmin,
                        //content: html
                    });
                }
            });
        })
    }

    //5.如果传过来的是字符串，代表调用方法。
    $.fn.formFormulaEditor.methods = {
        options: function (jq) {
            var opts = $.data(jq[0], 'formFormulaEditor').options;
            return opts;
        },
        getValue: function (jq) {
            return jq.html().replace(/&lt;/g, '<').replace(/&gt;/g, '>');
        },
        setValue: function (jq, param) {
            jq.html(param.replace(/</g, '&lt;').replace(/>/g, '&gt;'));
        },
        setCode: function (jq, param) {
            var opts = $.data(jq[0], 'formFormulaEditor').options;
            opts.UICode = param;
        }
    };

    function getSelectionCoords() {
        var sel = document.selection, range, rect;
        var x = 0, y = 0;
        if (sel) {
            if (sel.type != "Control") {
                range = sel.createRange();
                range.collapse(true);
                x = range.boundingLeft;
                y = range.boundingTop;
            }
        } else if (window.getSelection) {
            sel = window.getSelection();
            if (sel.rangeCount) {
                range = sel.getRangeAt(0).cloneRange();
                if (range.getClientRects) {
                    range.collapse(true);
                    if (range.getClientRects().length > 0) {
                        rect = range.getClientRects()[0];
                        x = rect.left;
                        y = rect.top;
                    }
                }
                // Fall back to inserting a temporary element
                if (x == 0 && y == 0) {
                    var span = document.createElement("span");
                    if (span.getClientRects) {
                        // Ensure span has dimensions and position by
                        // adding a zero-width space character
                        span.appendChild(document.createTextNode("\u200b"));
                        range.insertNode(span);
                        rect = span.getClientRects()[0];
                        x = rect.left;
                        y = rect.top;
                        var spanParent = span.parentNode;
                        spanParent.removeChild(span);

                        // Glue any broken text nodes back together
                        spanParent.normalize();
                    }
                }
            }
        }
        return { x: x, y: y };
    }

    function insertHtmlAtCaret(html) {
        var sel, range;
        if (window.getSelection) {
            sel = window.getSelection();
            if (sel.getRangeAt && sel.rangeCount) {
                range = sel.getRangeAt(0);
                range.deleteContents();
                var el = document.createElement("div");
                el.innerHTML = html;
                var frag = document.createDocumentFragment(), node, lastNode;
                while ((node = el.firstChild)) {
                    lastNode = frag.appendChild(node);
                }
                range.insertNode(frag);
                if (lastNode) {
                    range = range.cloneRange();
                    range.setStartAfter(lastNode);
                    range.collapse(true);
                    sel.removeAllRanges();
                    sel.addRange(range);
                }
            }
        } else if (document.selection && document.selection.type != "Control") {
            // IE < 9
            document.selection.createRange().pasteHTML(html);
        }
    }

    //6.默认参数列表
    $.fn.formFormulaEditor.defaults = {
        height: 260,
        width: 80,
        UICode:'',
    };
})(jQuery);
//------------------------------formFormulaEditor--------------------------------------end
//-------------------------------input-------------------------------------------------start
(function ($) {
    $.fn.extend({
        insertAtCaret: function (myValue) {
            var $t = $(this)[0];
            if (document.selection) {
                this.focus();
                sel = document.selection.createRange();
                sel.text = myValue;
                this.focus();
            }
            else
                if ($t.selectionStart || $t.selectionStart == '0') {
                    var startPos = $t.selectionStart;
                    var endPos = $t.selectionEnd;
                    var scrollTop = $t.scrollTop;
                    $t.value = $t.value.substring(0, startPos) + myValue + $t.value.substring(endPos, $t.value.length);
                    this.focus();
                    $t.selectionStart = startPos + myValue.length;
                    $t.selectionEnd = startPos + myValue.length;
                    $t.scrollTop = scrollTop;
                }
                else {
                    this.value += myValue;
                    this.focus();
                }
        }
    })
})(jQuery);
//-------------------------------input-------------------------------------------------end
